﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Windows;
using MediaAssistant.Controls.Dialog;
using MediaAssistant.Data;
using MefBasic;
using MefBasic.Commans;
using MediaAssistant.DAL;
using MediaAssistant.DAL.Constants;

namespace MediaAssistant.Controls.AlternativeLocation
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class AlternativeLocationPresenter : APresenter<AlternativeLocationView>, IDialogContentPresenter
    {
        [ImportingConstructor]
        public AlternativeLocationPresenter(AlternativeLocationView view)
            : base(view)
        {
            SelectedMovieChangedCommand = new DelegateCommand(ExecuteSelectedMovieChanged);
            MakeDefaultLocationCommand = new DelegateCommand(ExecuteMakeDefaultLocation, CanExecuteMakeDefaultLocation);
        }

        private bool CanExecuteMakeDefaultLocation(object arg)
        {
            return SelectedMovie != null;
        }

        public DelegateCommand MakeDefaultLocationCommand { get; set; }

        private void ExecuteMakeDefaultLocation(object obj)
        {
            var oldDefaultLocation = Movie.FullPath;
            DatabaseManager.Instance.MakeDefaultLocation(Movie, SelectedMovie.FullPath);
            SelectedMovie.FullPath = oldDefaultLocation;
            var fileInfo = new FileInfo(oldDefaultLocation);
            SelectedMovie.FileName = fileInfo.Name;
            SelectedMovie.FullPath = oldDefaultLocation;
            SelectedMovie.Size = fileInfo.Length;
            SelectedMovie.SizeText = Utility.GetFileSize(fileInfo.Length);
        }

        public DelegateCommand SelectedMovieChangedCommand { get; set; }

        private void ExecuteSelectedMovieChanged(object obj)
        {
            SelectedMovie = (Movie) obj;
            MakeDefaultLocationCommand.RaiseCanExecuteChanged();
        }

        public bool OnClosing(DialogResult dialogResult)
        {
            return true;
        }



        public Movie SelectedMovie
        {
            get { return (Movie)GetValue(SelectedMovieProperty); }
            set { SetValue(SelectedMovieProperty, value); }
        }

        public static readonly DependencyProperty SelectedMovieProperty =
            DependencyProperty.Register("SelectedMovie", typeof(Movie), typeof(AlternativeLocationPresenter), new UIPropertyMetadata(null));



        [Import]
        public LibraryDataSource DataSource
        {
            get { return (LibraryDataSource)GetValue(DataSourceProperty); }
            set { SetValue(DataSourceProperty, value); }
        }

        public static readonly DependencyProperty DataSourceProperty =
            DependencyProperty.Register("DataSource", typeof(LibraryDataSource), typeof(AlternativeLocationPresenter), new UIPropertyMetadata(null));




        public Movie Movie
        {
            get { return (Movie)GetValue(MovieProperty); }
            set { SetValue(MovieProperty, value); }
        }

        public static readonly DependencyProperty MovieProperty =
            DependencyProperty.Register("Movie", typeof(Movie), typeof(AlternativeLocationView), new UIPropertyMetadata(null));



        public ObservableCollection<Movie> AlternativeMovies
        {
            get { return (ObservableCollection<Movie>)GetValue(AlternativeMoviesProperty); }
            set { SetValue(AlternativeMoviesProperty, value); }
        }

        public static readonly DependencyProperty AlternativeMoviesProperty =
            DependencyProperty.Register("AlternativeMovies", typeof(ObservableCollection<Movie>), typeof(AlternativeLocationView), new UIPropertyMetadata(null));



        public void ShowAlternativeLocations(Movie movie)
        {
            Movie = movie;
            AlternativeMovies = new ObservableCollection<Movie>();
            var index = 1;
            foreach (var alternativeLocation in movie.AlternativeLocations)
            {
                if(File.Exists(alternativeLocation.Location)==false)
                {
                    continue;
                }
                var fileInfo = new FileInfo(alternativeLocation.Location);
                var alternativeMovie = new Movie
                {
                    Id = Guid.NewGuid().ToString(),
                    FileName = fileInfo.Name,
                    FullPath = alternativeLocation.Location,
                    Size = fileInfo.Length,
                    SizeText = Utility.GetFileSize(fileInfo.Length),
                    CreateDate = DateTime.Now,
                    Status = MovieStatus.Pending,
                    Title = movie.Title,
                    PlayCount = 0,
                    WeightedRating = 0,
                    Index = index++
                };
                AlternativeMovies.Add(alternativeMovie);
            }
            if (AlternativeMovies.Count==0)
                return;
            SelectedMovie = AlternativeMovies.First();
            if(Resolve<IDialogPresenter>().ShowDialog("Alternative Locations", this)==DialogResult.Ok)
            {
                foreach (var alternativeMovie in AlternativeMovies.Where(m=>string.IsNullOrEmpty(m.Title)==false))
                {
                    var fullPath = alternativeMovie.FullPath;
                    DatabaseManager.Instance.Delete(movie.AlternativeLocations.First(l => l.Location == fullPath));
                    DatabaseManager.Instance.AddMovie(alternativeMovie);
                }
                DatabaseManager.Instance.UpdateProcessingTitle(DataSource.ShowDrivesMoviesOnly);
            }
        }
    }
}
